package main

import (
	"context"
	"fmt"
	"time"
)

func go1(ctx context.Context) {

	for {
		select {
		case <-ctx.Done():
			fmt.Println("go1 done")
			return
		default:
			fmt.Println("go1 cpu的信息")
			time.Sleep(1 * time.Second)
		}

	}
}

func go2(ctx context.Context) {

	for {
		select {
		case <-ctx.Done():
			fmt.Println("go2 done")
			return
		default:
			fmt.Println("go2 cpu的信息")
			time.Sleep(1 * time.Second)
		}

	}
}
func go3(ctx context.Context) {

	value := ctx.Value("key")
	fmt.Println(value)
	for {
		select {
		case <-ctx.Done():
			fmt.Println("go3 done")
			return
		default:
			fmt.Println("go3 cpu的信息")
			time.Sleep(1 * time.Second)
		}
	}
}

func m2() {
	context1, cancel := context.WithCancel(context.Background())
	context2, _ := context.WithCancel(context1)
	context3, _ := context.WithCancel(context2)
	ctx4, _ := context.WithTimeout(context3, 3*time.Second)
	ctx5 := context.WithValue(ctx4, "key", "val")
	go go1(ctx4)
	go go2(context3)
	go go3(ctx5)
	time.Sleep(5 * time.Second)
	cancel()
	time.Sleep(1 * time.Second)
}
